package com.zztl.infiniteBand.ui.activity

import android.support.v7.widget.LinearLayoutManager

import android.text.TextUtils

import com.andview.refreshview.XRefreshView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.adapter.RanklistAdapter
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.model.BaseResponseBean
import com.zztl.infiniteBand.model.RanklistBean
import com.zztl.infiniteBand.net.requestBean.RanklistRequest
import com.zztl.infiniteBand.presenter.RankingPresenter
import com.zztl.infiniteBand.ui.view.mvpview.RankingListView
import com.zztl.infiniteBand.ui.view.recycleview.SimpleItemDecoration
import com.zztl.infiniteBand.util.DataFormatUtil
import com.zztl.infiniteBand.util.GlideCircleTransform
import kotlinx.android.synthetic.main.activity_rank_list.*

class RankListActivity : BaseMvpActivity<RankingListView,RankingPresenter>(), RankingListView {
/*    @BindView(R.id.tv_unit)
   lateinit  var mTvUnit: TextView
    @BindView(R.id.iv_user_header)
    lateinit var iv_user_header: ImageView
    @BindView(R.id.tv_name)
    lateinit var tv_name: TextView
    @BindView(R.id.tv_ranking)
    lateinit var tv_ranking: TextView
    @BindView(R.id.tv_hhh_money)
    lateinit var tv_hhh_money: TextView
    @BindView(R.id.recycleView)
    lateinit var recycleView: RecyclerView
    @BindView(R.id.xrefreshview)
    lateinit var xrefreshview: XRefreshView*/

    private var mRanklistAdapter: RanklistAdapter? = null
    private val mRankList = ArrayList<RanklistBean.DataBean>()
    lateinit var mRankingPresenter: RankingPresenter
    private var page = 1
    private var mRequest: RanklistRequest? = null

    override fun setLayoutId(): Int =R.layout.activity_rank_list


    override fun initBase() {
        setTitle(getString(R.string.收益排行榜))
        initRecyclerView()
        mRequest = RanklistRequest(page)
        mRankingPresenter!!.getRanking(mRequest!!, this)
    }

    private fun initRecyclerView() {
        recycleView.layoutManager = LinearLayoutManager(this)
        mRanklistAdapter = RanklistAdapter(R.layout.item_ranking, mRankList)
        recycleView.adapter = mRanklistAdapter
        recycleView.addItemDecoration(SimpleItemDecoration(this))
        xrefreshview.setPinnedTime(800)
        xrefreshview.pullLoadEnable = true
        xrefreshview.setAutoLoadMore(false)
        xrefreshview.setXRefreshViewListener(object : XRefreshView.SimpleXRefreshListener() {

            override fun onRefresh(isPullDown: Boolean) {
                page = 1
                mRequest!!.page = page
                mRankingPresenter.getRanking(mRequest!!, this@RankListActivity)

            }

            override fun onLoadMore(isSilence: Boolean) {
                mRequest!!.page = ++page
                mRankingPresenter.getRanking(mRequest!!, this@RankListActivity)
            }
        })
    }

    override fun createPresenter(): RankingPresenter {
        mRankingPresenter = RankingPresenter()
        return mRankingPresenter
    }


    override fun getListSuccess(bean: BaseResponseBean<*>) {
        val ranklistBean = bean as RanklistBean
        val list = ranklistBean.data!! as ArrayList
        if (list.size > 0) {
            if (page == 1) {
                val dataBean = list.removeAt(0)
                setMyUi(dataBean)
                mRanklistAdapter!!.setNewData(list)
            } else {
                //                list.remove(0);
                mRanklistAdapter!!.addData(list)
            }
        }
    }

    override fun stopRefershOrLoad() {
        xrefreshview.stopRefresh()
        xrefreshview.stopLoadMore(true)
    }


    private fun setMyUi(dataBean: RanklistBean.DataBean) {
        if (!TextUtils.isEmpty(dataBean.photo)) {
            setHeaderImg(dataBean.photo)
        }
        tv_name.text = dataBean.name
        if (dataBean.coin == 0.0) {
            tv_ranking.text = getString(R.string.未上榜)
        } else {
            if (dataBean.rank > 1000 || dataBean.rank == 0) {
                tv_ranking.text = getString(R.string.大于1000名)
            } else {
                tv_ranking.text = String.format(getString(R.string.排名), dataBean.rank)
            }
        }
        tv_hhh_money.text = DataFormatUtil.converTxtPoint4(dataBean.coin)
    }

    private fun setHeaderImg(imgUrl: String?) {
        if (TextUtils.isEmpty(imgUrl)) {
            return
        }
        Glide.with(this)
                .load(imgUrl)
                .apply(RequestOptions().placeholder(R.mipmap.ic_defaultphoto).transform(GlideCircleTransform(this)))
                .into(iv_user_header)
    }
}
